import { takeEvery } from "redux-saga/effects";
import { NATIVE_CLASS } from "@common/constants";
import { callNativeFunc, asyncRequest } from "@common/saga-helper";
import { TenantApi, UserApi } from "@api";
import {
  SAGA_USER_PROFILE_INIT_LOAD_DEFAULT_SCENARIO,
  SAGA_USER_PROFILE_LOAD_DEFAULT_SCENARIO,
  SAGA_USER_PROFILE_ADD_DEFAULT_WAREHOUSE,
  SAGA_USER_PROFILE_DEL_DEFAULT_WAREHOUSE,
  SAGA_USER_PROFILE_LOAD_TENANTS,
  SAGA_USER_PROFILE_SWITCH_TENANT,
  SAGA_USER_PROFILE_MODIFY_PWD,
} from "./saga-types";
import {
  SET_USER_PROFILE_DEFAULT_SCENARIO,
  SET_USER_PROFILE_ALL_WAREHOUSES,
  SET_USER_PROFILE_TENANT_LIST,
} from "./action-types";
import { UPDATE_TOKEN } from "../global-state/action-types";

function* initLoadUserProfileDefaultScenarioSaga() {
  yield takeEvery(
    SAGA_USER_PROFILE_INIT_LOAD_DEFAULT_SCENARIO,
    function* (action) {
      yield* callNativeFunc({
        effects: [
          {
            payload: {
              classFullName: NATIVE_CLASS.UserProfileHandler,
              action: "loadDefaultScenario",
              needNonce: false,
              params: action.payload,
            },
            respActions: [SET_USER_PROFILE_DEFAULT_SCENARIO],
          },
          {
            payload: {
              classFullName: NATIVE_CLASS.UserProfileHandler,
              action: "loadAllWarehouses",
              needNonce: false,
              params: action.payload,
            },
            respActions: [SET_USER_PROFILE_ALL_WAREHOUSES],
          },
        ],
      });
    }
  );
}

function* loadUserProfileDefaultScenarioSaga() {
  yield takeEvery(SAGA_USER_PROFILE_LOAD_DEFAULT_SCENARIO, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.UserProfileHandler,
            action: "loadDefaultScenario",
            needNonce: false,
            params: action.payload,
          },
          respActions: [SET_USER_PROFILE_DEFAULT_SCENARIO],
          callback: action.payload.callback,
        },
      ],
    });
  });
}

function* addUserProfileDefaultWarehouseSaga() {
  yield takeEvery(SAGA_USER_PROFILE_ADD_DEFAULT_WAREHOUSE, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.UserProfileHandler,
            action: "addDefaultWarehouse",
            needNonce: false,
            params: action.payload,
          },
          callback: action.payload.callback,
        },
        {
          payload: {
            classFullName: NATIVE_CLASS.UserProfileHandler,
            action: "loadDefaultScenario",
            needNonce: false,
            params: action.payload,
          },
          respActions: [SET_USER_PROFILE_DEFAULT_SCENARIO],
        },
      ],
    });
  });
}

function* delUserProfileDefaultWarehouseSaga() {
  yield takeEvery(SAGA_USER_PROFILE_DEL_DEFAULT_WAREHOUSE, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.UserProfileHandler,
            action: "delDefaultWarehouse",
            needNonce: false,
            params: action.payload,
          },
          callback: action.payload.callback,
        },
        {
          payload: {
            classFullName: NATIVE_CLASS.UserProfileHandler,
            action: "loadDefaultScenario",
            needNonce: false,
            params: action.payload,
          },
          respActions: [SET_USER_PROFILE_DEFAULT_SCENARIO],
        },
      ],
    });
  });
}

function* loadUserProfilesTenantsSaga() {
  yield takeEvery(SAGA_USER_PROFILE_LOAD_TENANTS, function* (action) {
    yield* asyncRequest({
      effects: [
        {
          api: TenantApi.loadByAcct,
          respActions: [SET_USER_PROFILE_TENANT_LIST],
          callback: action.payload.callback,
        },
      ],
    });
  });
}

function* switchUserProfileTenantSaga() {
  yield takeEvery(SAGA_USER_PROFILE_SWITCH_TENANT, function* (action) {
    yield* asyncRequest({
      effects: [
        {
          api: TenantApi.switchTenant,
          payload: action.payload,
          respActions: [UPDATE_TOKEN],
        },
      ],
    });
  });
}

function* modifyUserProfilePwdSaga() {
  yield takeEvery(SAGA_USER_PROFILE_MODIFY_PWD, function* (action) {
    yield* asyncRequest({
      effects: [
        {
          api: UserApi.modifyPwd,
          payload: action.payload,
          callback: action.payload.callback,
        },
      ],
    });
  });
}

export default [
  initLoadUserProfileDefaultScenarioSaga(),
  loadUserProfileDefaultScenarioSaga(),
  addUserProfileDefaultWarehouseSaga(),
  delUserProfileDefaultWarehouseSaga(),
  loadUserProfilesTenantsSaga(),
  switchUserProfileTenantSaga(),
  modifyUserProfilePwdSaga(),
];
